<?php

namespace App\Admin\Controllers;

use App\Models\Order;

use DeepCopy\Filter\Filter;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class OrderController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Order::class, function (Grid $grid) {
            $grid->model()->orderBy('id','desc');

            $grid->id('ID')->sortable();
            $grid->sn('订单号');
            $grid->username('姓名');
            $grid->mobile('电话');
            $grid->disableCreation();
            $grid->filter(function(Grid\Filter $filter){
                // 如果过滤器太多，可以使用弹出模态框来显示过滤器.
//                $filter->useModal();
                // 禁用id查询框
                $filter->disableIdFilter();
                $filter->like('sn', '订单号');
            });
            $grid->pay_time('购买时间');
            $grid->column("","地址")->display(function(){
                return $this->address . ' '.$this->street;
            });
            $grid->actions(function(Grid\Displayers\Actions $actions){
                $actions->disableDelete();
            });
            $grid->created_at('创建时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Order::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
